package com.yantong.service;

import com.yantong.config.LocalRedis;
import com.yantong.constant.StringConstans;
import com.yantong.utils.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@Service
public class CacheDataService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CacheDataService.class);
    public static final String CACHE_PREFIX = "cache_";
    public static final String CACHE_PREFIX_CONFIG = CACHE_PREFIX + "config_";
    public static final String CACHE_PREFIX_COMMON = CACHE_PREFIX + "common_";
    public static final String CACHE_PREFIX_WX = CACHE_PREFIX + "wx_";

    @Autowired
    private LocalRedis localRedis;

    public String getConfigCacheByKey(String key) {
        String cacheKey = CACHE_PREFIX_CONFIG + key;
        String configValue = localRedis.get(cacheKey, String.class);
        if (ValidateUtil.isEmptyString(configValue)) {
//            configValue = configService.getConfigStrByKey(key);
            //值存在，设置到缓存中
            if (ValidateUtil.isNotEmptyString(configValue)) {
                localRedis.put(cacheKey, configValue);
            }
        }
        return configValue;
    }

    public Double getConfigDoubleCacheByKey(String key) {
        String cacheKey = CACHE_PREFIX_CONFIG + key;
        Double result = 0d;
        String configValue = getConfigCacheByKey(cacheKey);
        //值存在
        if (ValidateUtil.isNotEmptyString(configValue)) {
            result = Double.parseDouble(configValue);
        }
        return result;
    }

    /**
     * 获取key对应的Bigdecimal值
     *
     * @param key 获取的值对应的key
     * @return 返回值
     */
    public BigDecimal getConfigBigDecimalValue(String key) {
        String keyStringValue = getConfigCacheByKey(key);
        if (ValidateUtil.isEmptyString(keyStringValue)) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(keyStringValue);
    }

    /**
     * 获取key对应的boolean值：系统中统一1为true，0或者null为false
     *
     * @param key 获取的值对应的key
     * @return 返回值
     */
    public boolean getBooleanValue(String key) {
        String keyStringValue = getConfigCacheByKey(key);
        return StringConstans.STR_ONE.equals(keyStringValue);
    }

    public void setCache(String key, String value) {
        localRedis.set(key, value);
    }

    public Object getCacheObjeByKey(String key) {
        return localRedis.get(key);
    }

    public String getStringValue(String key) {
        Object objValue = getCacheObjeByKey(key);
        String result = StringConstans.STR_EMPTY;
        if (null != objValue) {
            result = objValue.toString();
        }
        return result;
    }

    private String getCommonCacheKey(String key) {
        return CACHE_PREFIX_COMMON + key;
    }

    public void setCache(String key, String value, long time, TimeUnit timeUnit) {
        if (time > 0) {
            localRedis.set(key, value, time, timeUnit);
        } else {
            localRedis.set(key, value);
        }
    }

    public void setCommonCache(String key, String value) {
        setCache(getCommonCacheKey(key), value);
    }

    public void setCommonCache(String key, String value, long time,TimeUnit timeUnit) {
        setCache(getCommonCacheKey(key), value, time,timeUnit);
    }

    public String getCacheByKey(String key) {
        return localRedis.get(key, String.class);
    }

    public String getCommonCacheByKey(String key) {
        return localRedis.get(getCommonCacheKey(key), String.class);
    }

}
